<!-- 基础表格组件 -->
<template>
  <div class="baseList">
    <div class="tableBoxs">
      <div class="newBox">
        <button class="bt wt-120" @click="showForm">新增护理等级</button>
      </div>
      <t-table
        :data="listData"
        :columns="COLUMNS"
        :row-key="rowKey"
        vertical-align="middle"
        :hover="true"
        :loading="dataLoading"
        table-layout="fixed"
        table-content-width="100%"
      >
        <!-- 序号 -->
        <template #rowIndex="{ rowIndex }">{{ rowIndex + 1 }}</template>
        <!-- end -->
        <!-- 护理费用 -->
        <template #fee="{ row }">{{
          isDecimals(row.fee) ? row.fee : row.fee + '.00'
        }}</template>
        <!-- end -->
        <!-- 操作栏 -->
        <template #op="{ row }">
          <div class="operateCon">
            <a
              class="btn-dl"
              :class="row.cid ? 'disabled' : ''"
              @click="handleDelete(row.id)"
              >删除</a
            >
            <a
              class="font-bt"
              :class="row.cid ? 'disabled' : ''"
              @click="handleEdit(row.id)"
              >编辑</a
            >
            <a
              :class="row.status === 1 ? 'btn-dl' : 'font-bt'"
              @click="handleStatus(row.id,row.status)"
              >{{ row.status === 1 ? '禁用' : '启用' }} </a
            >
          </div>
        </template>
        <!-- end -->
        <!-- 暂无数据 -->
        <template #empty>
          <NoData></NoData>
        </template>
        <!-- end -->
      </t-table>
      <!-- 分页 -->
      <t-pagination
        v-if="total > 5"
        v-model="pagination.pageNum"
        v-model:pageSize="pagination.pageSize"
        :total="total"
        @change="handlePageChange"
      />
      <!-- end -->
    </div>
  </div>
</template>

<script setup lang="ts">
// 表格
import { COLUMNS } from '../constants' // 表格列
// 校验
import { isDecimals } from '@/utils/validate'
// 无数据时显示的提示
import NoData from '@/components/noData/index.vue'

import {levelStatus} from '@/api/serve'


import { MessagePlugin } from 'tdesign-vue-next'
// 获取父级数据
const props = defineProps(['listData', 'total', 'dataLoading', 'pagination'])
const emits = defineEmits(['getData','deleteLevel','handleEdit','openForm'])
// ------定义变量------
// 行的key
const rowKey = 'index'
const params ={
  id:0,
  status:null
}

function showForm(){
  emits('openForm')
}

function handlePageChange(pageInfo){
  props.pagination.pageSize = pageInfo.pageSize
  props.pagination.pageNum = pageInfo.current
  emits('getData')
}
function handleDelete(id){
  emits('deleteLevel',id)
}

async function handleStatus(id,status){
  params.id = id
  params.status = status==1?0:1
  console.log("params",params)
  await levelStatus(params).then(res => {
    if(res.code === 200){
      MessagePlugin.success(res.msg)
    }else{
      MessagePlugin.error(res.msg)
    }
  })
  emits('getData')
}

function handleEdit(id){
  emits('handleEdit',id)
}

</script>
